package cn.recyclerview;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;


class StuAdpater extends RecyclerView.Adapter<StuViewHolder> {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnChangeListener listener;

    public StuAdpater(Context context, ArrayList<Student> studentArrayList, IOnChangeListener listener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
    }

    @Override
    public StuViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);

        StuViewHolder viewHolder = new StuViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(StuViewHolder stuViewHolder, int i) {
        final Student s = studentArrayList.get(i);
        stuViewHolder.headImg.setImageResource(s.getImgId());
        stuViewHolder.nameView.setText(s.getName());
        stuViewHolder.ageView.setText(String.valueOf(s.getAge()));
        stuViewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.edit(s);
            }
        });
        stuViewHolder.delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(s.getNo());
            }
        });
    }


    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }

    /*返回搜索结果*/
    public void changeData(ArrayList<Student> studentArrayList) {
        this.studentArrayList = studentArrayList;
        notifyDataSetChanged();
    }
}
